<template>
  <div>
    <!-- 头部返回区域 -->
    <div class="header">
      <router-link to="/profile" tag="div" class="iconfont back_icon">&#xe616;</router-link>
      <h3>登录界面</h3>
    </div>
    <!-- 登录表单区域 -->
    <div class="main">
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form">
        <!-- 手机号 -->
        <el-form-item prop="tel">
          <el-input
            v-model="loginForm.tel"
            prefix-icon="iconfont iconyonghu"
            placeholder="请输入您的手机号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="iconfont iconmima"
            placeholder="请输入您的密码"
          ></el-input>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item class="login_btn">
          <el-button type="info" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import qs from 'qs'
export default {
  name: 'Login',
  data () {
    // 验证手机号码规则
    var checkPhone = (rule, value, callback) => {
      const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
      if (reg.test(value)) {
        callback()
      } else {
        return callback(new Error('请输入正确的手机号'))
      }
    }
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        tel: '',
        password: ''
      },
      // 表单的验证规则对象
      loginFormRules: {
        // 验证手机号是否合法
        tel: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async login () {
      // Qs.stringify()将对象序列化成URL的形式，Qs是axios里面自带的
      const str = qs.stringify(this.loginForm)
      const { data: res } = await this.$http.post('/api/user/login', str, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 将返回的信息保存在sessionStorage中
      window.sessionStorage.setItem('user', JSON.stringify({ user_id: res.data.user_id, username: res.data.username, user_sex: res.data.user_sex, user_tel: res.data.user_tel }))
      // 保存登录信息
      this.$store.commit('login')
      // 跳转到个人中心页面
      this.$router.push('/profile')
    }
  }
}
</script>

<style lang="less" scoped>
// 头部区域
.header {
  position: relative;
  width: 100%;
  height: 1.6rem;
  line-height: 1.6rem;
  font-size: .72rem;
  text-align: center;
  background: #fff;
  // 返回按钮
  .back_icon {
    position: absolute;
    left: .4rem;
    top: 50%;
    transform: translateY(-50%);
    font-size: .8rem;
  }
}
// 登录表单区域
.main {
  padding: .4rem;
}
.login_form {
  /deep/ .el-input__inner {
    border: 0;
  }
  /deep/ .el-radio__inner {
    width: 0;
    height: 0;
    border: 0;
    border-radius: 0;
    background-color:#A8A8A8;
  }
  /deep/ .el-radio__label {
    font-size: 0;
  }
  /deep/ .el-form-item__content {
    line-height: 0.44rem;
  }
}
.iconwebicon19 {
  color: #d4237a;
}
.el-button--info {
  width: 100%;
}
</style>
